#include <stdio.h>

/* CHANGE THIS */
// BGR
unsigned char BGR[16] = {
 0x19, 0x0F, 0x10, 0x30, 0x19, 0x16, 0x26, 0x36,
 0x19, 0x0F, 0x18, 0x28, 0x19, 0x0F, 0x17, 0x07
};

// SPR
unsigned char SPR[16] = {
 0x19, 0x30, 0x21, 0x26, 0x19, 0x0F, 0x26, 0x30,
 0x19, 0x0F, 0x15, 0x30, 0x19, 0x0F, 0x21, 0x30
};
/* CHANGE THIS */

// real NES RGB pal
typedef struct
{
	unsigned char r, g, b;
} RGB_ENTRY;

RGB_ENTRY nes_palette[64] =
{
   {0x80,0x80,0x80}, {0x00,0x00,0xBB}, {0x37,0x00,0xBF}, {0x84,0x00,0xA6},
   {0xBB,0x00,0x6A}, {0xB7,0x00,0x1E}, {0xB3,0x00,0x00}, {0x91,0x26,0x00},
   {0x7B,0x2B,0x00}, {0x00,0x3E,0x00}, {0x00,0x48,0x0D}, {0x00,0x3C,0x22},
   {0x00,0x2F,0x66}, {0x00,0x00,0x00}, {0x05,0x05,0x05}, {0x05,0x05,0x05},

   {0xC8,0xC8,0xC8}, {0x00,0x59,0xFF}, {0x44,0x3C,0xFF}, {0xB7,0x33,0xCC},
   {0xFF,0x33,0xAA}, {0xFF,0x37,0x5E}, {0xFF,0x37,0x1A}, {0xD5,0x4B,0x00},
   {0xC4,0x62,0x00}, {0x3C,0x7B,0x00}, {0x1E,0x84,0x15}, {0x00,0x95,0x66},
   {0x00,0x84,0xC4}, {0x11,0x11,0x11}, {0x09,0x09,0x09}, {0x09,0x09,0x09},

   {0xFF,0xFF,0xFF}, {0x00,0x95,0xFF}, {0x6F,0x84,0xFF}, {0xD5,0x6F,0xFF},
   {0xFF,0x77,0xCC}, {0xFF,0x6F,0x99}, {0xFF,0x7B,0x59}, {0xFF,0x91,0x5F},
   {0xFF,0xA2,0x33}, {0xA6,0xBF,0x00}, {0x51,0xD9,0x6A}, {0x4D,0xD5,0xAE},
   {0x00,0xD9,0xFF}, {0x66,0x66,0x66}, {0x0D,0x0D,0x0D}, {0x0D,0x0D,0x0D},

   {0xFF,0xFF,0xFF}, {0x84,0xBF,0xFF}, {0xBB,0xBB,0xFF}, {0xD0,0xBB,0xFF},
   {0xFF,0xBF,0xEA}, {0xFF,0xBF,0xCC}, {0xFF,0xC4,0xB7}, {0xFF,0xCC,0xAE},
   {0xFF,0xD9,0xA2}, {0xCC,0xE1,0x99}, {0xAE,0xEE,0xB7}, {0xAA,0xF7,0xEE},
   {0xB3,0xEE,0xFF}, {0xDD,0xDD,0xDD}, {0x11,0x11,0x11}, {0x11,0x11,0x11}
};

#define	BMP_NAME		"pal0.bmp"
#define	WIDTH			16*16
#define	HEIGHT		16*2
#define	FSIZE			(WIDTH * HEIGHT * 3) + 0x36;

typedef struct
{
	unsigned char b, g, r;
} PIXEL;

int main()
{
	FILE *bmp;
	unsigned char hdr[0x36];
	int i, r, entry;

	bmp = fopen(BMP_NAME, "wb");

	// hardcoded header
	hdr[0] = 'B'; hdr[1] = 'M';
	*(unsigned long *)(hdr+2) = FSIZE;
	*(unsigned long *)(hdr+6) = 0;
	*(unsigned long *)(hdr+10) = 0x36;
	*(unsigned long *)(hdr+14) = 0x28;
	*(unsigned long *)(hdr+18) = WIDTH;
	*(unsigned long *)(hdr+22) = HEIGHT;
	*(unsigned short *)(hdr+26) = 1;
	*(unsigned short *)(hdr+28) = 24;
	*(unsigned long *)(hdr+30) = 0;
	*(unsigned long *)(hdr+34) = FSIZE - 0x36;
	*(unsigned long *)(hdr+38) = 0x0ec4;
	*(unsigned long *)(hdr+42) = 0x0ec4;
	*(unsigned long *)(hdr+46) = 0;
	*(unsigned long *)(hdr+50) = 0;
	fwrite(hdr, 1, 0x36, bmp);

	// vertical flip
	// SPR pal image
	for(r=0; r<16; r++)
	for(entry=0; entry<16; entry++)
	{
		PIXEL p;
		p.r = nes_palette[SPR[entry]].r;
		p.g = nes_palette[SPR[entry]].g;
		p.b = nes_palette[SPR[entry]].b;
		for(i=0; i<16; i++)
			fwrite(&p, 1, 3, bmp);
	}

	// BGR pal image
	for(r=0; r<16; r++)
	for(entry=0; entry<16; entry++)
	{
		PIXEL p;
		p.r = nes_palette[BGR[entry]].r;
		p.g = nes_palette[BGR[entry]].g;
		p.b = nes_palette[BGR[entry]].b;
		for(i=0; i<16; i++)
			fwrite(&p, 1, 3, bmp);
	}
}